package com.tykboot.controller;

import com.tykboot.core.base.BaseController;
import com.tykboot.core.persistence.Criteria;
import com.tykboot.core.persistence.Restrictions;
import com.tykboot.entity.TbBeneficiary;
import com.tykboot.message.ResponseMsg;
import com.tykboot.service.ITbAccountDetailService;
import com.tykboot.service.ITbAccountServicerService;
import com.tykboot.service.ITbBeneficiaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  Controller
 *
 * @author LiuYu
 */ 
@Api(tags = "beneficiary", description = "/api/beneficiaries")
@RestController
@RequestMapping(BaseController.API + "/beneficiaries")
public class TbBeneficiaryController extends BaseController {

	@Autowired
	private ITbBeneficiaryService tbBeneficiaryService;

	@Autowired
	private ITbAccountDetailService tbAccountDetailService;

	@Autowired
	private ITbAccountServicerService tbAccountServicerService;

	@ApiOperation(value = "Get Beneficiaries", notes = "Get Beneficiaries related to an financialId")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true)
	})
	@GetMapping("")
	public ResponseMsg<List<TbBeneficiary>> queryBeneficiaries(@RequestHeader("financialId") String financialId) {
		ResponseMsg<List<TbBeneficiary>> responseMsg = new ResponseMsg<>();
		Criteria<TbBeneficiary> req = new Criteria<>();
		req.add(Restrictions.eq("financialId", financialId));
		List<TbBeneficiary> list = tbBeneficiaryService.findAll(req);
		if (list != null && !list.isEmpty()) {
			for(TbBeneficiary model : list) {
				model.setCreditorAccount(tbAccountDetailService.findById(model.getAccountId()));
				model.setServicer(tbAccountServicerService.findById(model.getAccountId()));
			}
			responseMsg.setData(list);
		}
		return responseMsg;
	}

	@ApiOperation(value = "Get account Beneficiaries", notes = "Get Beneficiaries related to an account")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true),
			@ApiImplicitParam(name = "accountId", value = "accountId", dataType = "string", paramType = "path", required = true)
	})
	@GetMapping("/{accountId}")
	public ResponseMsg<List<TbBeneficiary>> queryBeneficiariesById(@RequestHeader("financialId") String financialId,
																   @PathVariable("accountId") String accountId) {
		ResponseMsg<List<TbBeneficiary>> responseMsg = new ResponseMsg<>();
		Criteria<TbBeneficiary> req = new Criteria<>();
		req.add(Restrictions.eq("accountId", accountId));
		req.add(Restrictions.eq("financialId", financialId));
		List<TbBeneficiary> list = tbBeneficiaryService.findAll(req);
		if (list != null && !list.isEmpty()) {
			for(TbBeneficiary model : list) {
				model.setCreditorAccount(tbAccountDetailService.findById(accountId));
				model.setServicer(tbAccountServicerService.findById(model.getAccountId()));
			}
			responseMsg.setData(list);
		}
		return responseMsg;
	}

}
